﻿@model Umbraco.Cms.Web.Model.BackOffice.Editors.BasicContentEditorModel
@{
    Layout = "_TabbedEditorLayout.cshtml";
}
@section Head {
    @RenderSection("Head", false)
}
@section Tabs {
    @if (IsSectionDefined("Tabs"))
    {
        @RenderSection("Tabs")
    }
    else
    {
        foreach (var t in Model.SortedTabs)
        {
            <li><a href="#t-@t.Id.ToString()"><span>@t.Name</span></a></li>
        }
        <li><a href="#t-umb-properties"><span>Umbraco Properties</span></a></li>   
    }
}
@using (Html.BeginForm((string)ViewBag.Action, (string)ViewBag.Controller, (object)ViewBag.UrlParams, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.UmbValidationSummary("Please correct the errors and try again.")
                   
    @Html.HiddenFor(x => Model.ActiveTabIndex)
    @Html.HiddenFor(x => Model.ParentId)
    
    if (IsSectionDefined("EditorBar"))
    {
        @RenderSection("EditorBar")
    }
    else
    {
        @Html.UmbEditorBar();
    }
            
    <div id="editorContent" class="has-editor-bar scrolling-content padded-content">
        
        @Html.DisplayFor(x => Model.NoticeBoard)
        
        @{foreach (var t in Model.SortedTabs.DistinctBy(x => x.Alias))
          {
            <div>
                @* render editor for the current property for the current tab *@
                
                @{
                    var contentProperties = Model.Properties.Where(x => x.TabAlias == t.Alias).ToArray();

                    foreach (var p in contentProperties.OrderBy(x => x.SortOrder).ThenBy(x => x.DocTypeProperty.SchemaName))
                    {
                        @Html.EditorFor(editor => p, "ContentProperty", p.Id.GetHtmlId())
                    }
                }
            </div>
          }
        }
        <div>
            
            @if (IsSectionDefined("UmbracoProperties"))
            {
                @RenderSection("UmbracoProperties")
            }
            else
            {
                <div class="property-pane clearfix">
                    @Html.EditorFor(x => Model, "CoreEntityModel")
                </div>
            }                
            
            
            @if (IsSectionDefined("GeneralProperties"))
            {
                @RenderSection("GeneralProperties")
            }
            else
            {
                //Get all properties that aren't assigned to tabs that are not our in-built ones
                var nonTabbedProps = Model.Properties
                    .Where(x => !Model.SortedTabs.Select(t => t.Id).Contains(x.TabId) && !x.Alias.StartsWith("system-"))
                    .ToArray();
                if (nonTabbedProps.Any())
                {
                    //render editor for the properties not found on a tab
                    //there generally won't be any of these
                    foreach (var p in nonTabbedProps)
                    {                            
                        @Html.EditorFor(editor => p, "ContentProperty", p.Id.GetHtmlId())
                    }
                }
            }
            
        </div>
    </div>
    
    @RenderBody()
}
